/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sree.tali.filter;

import com.sree.tali.buscomp.DisplayBusComp;
import com.sree.tali.buscomp.DisplayBusCompImpl;
import com.sree.tali.plugin.HibernatePlugin;
import com.sree.tali.pojo.LoginForm;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import org.hibernate.SessionFactory;

// Implements Filter class
public class UrlReWriteFilter implements Filter {
      private  ServletContext application= null;
    public void init(FilterConfig config)
            throws ServletException {
      // Get init parameter 
       application =config.getServletContext();
        //Print the init parameter 
    }

    public void doFilter(ServletRequest request,
            ServletResponse response,
            FilterChain chain)
            throws java.io.IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpResp = (HttpServletResponse) response;
        String uri = httpReq.getRequestURI();
        if(uri.toUpperCase().contains("TITLES/"))
        {
        String forwardUrl = "/showarticles.do?method=displayTitles&&articleType="+uri.substring(uri.lastIndexOf("/")+1);
        request.getRequestDispatcher(forwardUrl).forward(request, response);
        }
        else if (uri.toUpperCase().contains("USER/"))
        {
        String forwardUrl ="/showarticles.do?method=displayArticle&&articleName="+uri.substring(uri.lastIndexOf("/")+1);
        request.getRequestDispatcher(forwardUrl).forward(request, response);
         }
       else
            chain.doFilter(request,response);
        // Get the IP address of client machine.   
      /*  String ipAddress = request.getRemoteAddr();

        // Log the IP address and current timestamp.
        System.out.println("IP " + ipAddress);
        boolean isValid=false;
        if(!isValid){
        request.getRequestDispatcher("/SreeLogin.jsp").forward(request, response);
        }
        else
             chain.doFilter(request,response);
        */
        //response.getWriter().
    }

    public void destroy() {
        /* Called before the Filter instance is removed 
         from service by the web container*/
    }
}
